<?php

namespace App\Models;

use Encore\Admin\Traits\ModelTree;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Request;

class Department extends BaseModel
{
    protected $table = 'crm_department';

    use ModelTree;


    public function getEmployeeAttribute($value)
    {
        return json_decode($value);
    }

    public function setEmployeeAttribute($value)
    {
        return $this->attributes['employee'] = json_encode($value);
    }

    public function delete()
    {
        User::quitDepartment($this->employee);
        return parent::delete();
    }


    public static function getLevel($level)
    {
        $cacheFile = date('Ymd',time()).'-department-getLevel-'.$level;

        if(Redis::exists($cacheFile)) {
            $r_data = Redis::get($cacheFile);
        }else{
            $dp = self::query()->where('level',$level)->pluck('title','id')->toArray();
            $r_data = json_encode($dp);
            Redis::set($cacheFile,$r_data);
        }

        return json_decode($r_data, true);
    }

    public static function allDepartment()
    {
        $cacheFile = date('Ymd',time()).'-allDepartment-';

        if(Redis::exists($cacheFile)) {
            $r_data = Redis::get($cacheFile);
        }else{
            $dp = self::query()->pluck('title','id')->toArray();
            $r_data = json_encode($dp);
            Redis::set($cacheFile,$r_data);
        }

        return json_decode($r_data, true);
    }

    public static function updateLevel()
    {
        $first = self::query()->where('parent_id',0);
        $first_ids = $first->pluck('id');
        $first->update(['level'=>1]);

        $second = self::query()->whereIn('parent_id',$first_ids);
        $second_ids = $second->pluck('id');
        $second->update(['level'=>2]);

        $third = self::query()->whereIn('parent_id',$second_ids);
        $third_ids = $third->pluck('id');
        $third->update(['level'=>3]);

        $fourth = self::query()->whereIn('parent_id',$third_ids);
        $fourth_ids = $fourth->pluck('id');
        $fourth->update(['level'=>4]);

    }

    /**
     * 获取当前主管姓名
     * @param $employee_id
     * @return string
     */
    public static function getLeader($employee_id)
    {
        $cacheFile = date('Ymd',time()).'-getLeader-'.$employee_id;

        if(Redis::exists($cacheFile)) {
            $leader = Redis::get($cacheFile);
        }else{
            // 如果自己就是 leader，那就找父级
            $userExist = User::query()->where('id',$employee_id)->where('is_leader',1)->exists();
            if($userExist){
                // 自己就是主管，当前主管应该是上一级的leader
                $self_department_id = User::query()->where('id',$employee_id)->where('is_leader',1)->pluck('department_id')->first();
                // 获取当前部门的父级部门
                $parent = Department::query()->where('id', $self_department_id)->pluck('parent_id')->first();
                $parent = Department::query()->where('id', $parent)->pluck('leader')->first();
                $leader = $parent;
            }else{
                $self_department_id = User::query()->where('id',$employee_id)->pluck('department_id')->first();
                $parent = User::query()->where('department_id', $self_department_id)->where('is_leader',1)->pluck('id')->first();
                $leader = $parent;

            }
            Redis::set($cacheFile,$leader);
        }

        if(is_numeric($leader) && $leader > 0){
            return User::Users()[$leader];
        }
        return '';
    }

    /**
     * 获取当前主管下所有员工
     * @param $leader_id
     * @return array
     */
    public static function getEmployees($leader_id)
    {
        $cacheFile = date('Ymd',time()).'-getEmployees-'.$leader_id;
        if(Redis::exists($cacheFile)) {
            $r_data = Redis::get($cacheFile);
        }else{
            $department_ids = self::query()->where('leader',$leader_id)->pluck('id');
            $ids = $department_ids;
            while ($ids->count() > 0){
                $ids = self::query()->whereIn('parent_id',$ids)->pluck('id');
                foreach ($ids as $id){
                    $department_ids->add($id);
                }
            }
            $employee_ids = User::query()->where('department_id',$department_ids->toArray())->pluck('id')->toArray();
            $r_data = json_encode($employee_ids);
            Redis::set($cacheFile,$r_data);
        }
        return json_decode($r_data, true);
    }

    /***
     * @return mixed 关联 用户表 admin_user
     */
    public function employees()
    {
        return $this->hasMany(User::class, 'department_id', 'id');
    }


    public function children()
    {
        $this->hasMany(get_class($this) , 'parent_id' , 'id');
    }

    public function parent()
    {
        $this->hasMany(get_class($this) , 'parent_id' , 'id');
    }
}
